package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tongatong
 * @name BaseManageController
 * @description 菜单管理类
 * @date 2021/9/7
 * @time 18:54
 */
@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
public class BaseManageController {
    @Resource
    private ManageService manageService;

    /**
     * 获取一级分类数据
     * 接口路径：GET/admin/product/getCategory1
     * @return 所有一级分类数据
     */
    @ApiOperation(value = "获取一级分类数据")
    @GetMapping("getCategory1")
    public Result<List<BaseCategory1>> getCategory1() {
        System.out.println("BaseManageController");
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    /**
     * 根据一级分类Id,获取二级分类数据
     * 接口路径：GET/admin/product/getCategory2/{category1Id}
     * @return 所有二级分类数据
     */
    @ApiOperation(value = "获取二级分类数据")
    @GetMapping("getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getCategory2(@PathVariable("category1Id") Long category1Id) {
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    /**
     * 根据一级分类Id,获取三级分类数据
     * 接口路径：GET/admin/product/getCategory3/{category2Id}
     * @return 所有三级分类数据
     */
    @ApiOperation(value = "获取三级分类数据")
    @GetMapping("getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getCategory3(@PathVariable("category2Id") Long category2Id) {
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    /**
     * 根据分类Id 获取平台属性数据
     * @param category1Id 一级分类Id
     * @param category2Id 二级分类Id
     * @param category3Id 三级分类Id
     * @return 所有平台属性数据
     */
    @ApiOperation(value = "获取平台属性数据")
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> attrInfoList(@PathVariable("category1Id") Long category1Id,
                                                   @PathVariable("category2Id") Long category2Id,
                                                   @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = manageService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    @ApiOperation("保存平台属性")
    @PostMapping("saveAttrInfo")
    ///admin/product/saveAttrInfo
    public Result<Void> saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        //从前台获取数据，封装到对象baseAttrInfo中
        manageService.saveAttrInfo(baseAttrInfo);
       return Result.ok();
    }

    @ApiOperation("根据attrId，获取所有该属性的value值")
    @GetMapping("getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId) {
        BaseAttrInfo baseAttrInfo = manageService.getAttrInfo(attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);
    }

    /**
     * SKU分页列表
     * @param page 当前页
     * @param limit 每页显示膈俞
     * @return SKU列表
     */
    @ApiOperation("SKU分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result<IPage<SkuInfo>> getSKUListByPage(
            @PathVariable Long page,
            @PathVariable Long limit) {

        Page<SkuInfo> pageParam = new Page<>(page, limit);
        IPage<SkuInfo> pageModel = manageService.getPage(pageParam);
        return Result.ok(pageModel);
    }

    /**
     * 商品上架
     * @param skuId  skuid
     * @return 返回状态
     */
    @ApiOperation("商品上架")
    @GetMapping("onSale/{skuId}")
    public Result<Void> onSale(@PathVariable("skuId") Long skuId) {
        manageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架
     * @param skuId skuid
     * @return 返回状态
     */
    @ApiOperation("商品下架")
    @GetMapping("cancelSale/{skuId}")
    public Result<Void> cancelSale(@PathVariable("skuId") Long skuId) {
        manageService.cancelSale(skuId);
        return Result.ok();
    }


}
